import { ILoadMapTypeAll } from '@/common';
import { getBaseUrl } from '@/services/urlconst';
import { commonManageSearchParams, getLoadMapList, getSelectDataMapList, getTenantID, requestFunComponFiex } from '@/utils';
import { useRequest } from 'umi';
import { AssetacquisitionTabSwitch, ManageSearchColumns } from '../mconfig';
import { useCallback, useMemo, useState } from 'react';
import { ModalSearchColumns } from '../mconfig/ModalSearchColumns';

/** 弹窗获取计划 service */
export const MaterialManageServices = (curRes: Record<string, any>) => {
  const loadMdicIds: string[] = [`${getTenantID()}${ILoadMapTypeAll.ASSET_TYPE}`];
  /***获取数据字典**/
  const { loadMapInfo, mapLoading } = getLoadMapList([
    `${getTenantID()}${ILoadMapTypeAll.ASSET_INNENTORY_STATUS}`, //资产类型
  ]);

  const authority: any = AssetacquisitionTabSwitch(curRes);
  const assetType = getSelectDataMapList(loadMapInfo?.[`${getTenantID()}${ILoadMapTypeAll.ASSET_INNENTORY_STATUS}`], 'name', 'id', true);

  /** 获取搜索参数 */
  const searchListParams = ModalSearchColumns(assetType);
  /***保存当前的搜索参数**/
  const [manageTableParams, setManageTableParams] = useState<Record<string, any>>({});

  /***管理页获取数据方法**/
  const { tableProps, data, loading, run } = useRequest(
    (reqParams: any) => {
      let PostParams = {
        current: 1,
        pageSize: 10,
        dicTypeIds: loadMdicIds?.toString(),
        sort: 'createTime',
        order: 'desc',
        ...authority,
        ...reqParams,
      };

      const searchParams: Record<string, any> = commonManageSearchParams(searchListParams, PostParams, {});

      /***保存一份参数**/
      setManageTableParams(searchParams);

      let reqUrl;
      // 物料管理
      reqUrl = `${getBaseUrl()}/assetInventory/dataGrid/${authority.params}`;
      /***发起请求数据接口**/
      return requestFunComponFiex('POST', reqUrl, searchParams);
    },
    {
      manual: true,
      paginated: true,
      defaultPageSize: 10,
    },
  );

  /***重新请求的方法**/
  const reloadRequest = useCallback((reqParams: any) => {
    run({ ...reqParams });
  }, []);

  /***页面数据及分页信息**/
  const tableValues = useMemo(() => {
    return {
      data,
      pagination: tableProps?.pagination,
    };
  }, [data, tableProps]);

  /***搜索功能配置columns**/
  const searchList = useMemo(() => {
    return {
      searchList: ModalSearchColumns(assetType),
    };
  }, [manageTableParams, assetType]);

  return {
    loading,
    reloadRequest,
    manageTableParams,
    ...tableValues, //页面列表值
    ...searchList,
  };
};
